from PIL import Image, ImageDraw, ImageFont
import os


def draw_text(text_file):
    with open(text_file, 'r', encoding='utf-8') as file:
        text = file.read()
        file_name = file.name
    font = ImageFont.truetype('resources/font/simhei.ttf', size=12)
    image_width = font.getsize(max(text.split('\n'), key=len))[0] + 20
    image_height = font.getsize(text)[1] * (text.count('\n') + 6) + 20
    image = Image.new('RGB', (image_width, image_height), color='white')
    draw = ImageDraw.Draw(image)
    draw.text((10, 10), text, font=font, fill='black')
    image.save(f'{os.path.splitext(file_name)[0]}.png')
